<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    class Posts extends CI_Controller {

        function __construct(){
            parent::__construct();
            $this->data['page'] = isset($_GET['post_type'])? ucfirst($_GET['post_type']) : ucfirst($_POST['post_type']);
            $this->data['wp_languages'] = array(
                'me' => 'Crnogorski',
                'en' => 'English',
                'ru' => 'Rусский ',
                'fr' => 'Français',
                'al' => 'Albanian'
            ); 
            /*
            * Class Translate
            * set local language
            */
            $this->load->library('translate');
            $this->translate->setLang('me');
            $this->load->model('posts_model');
        }

        function index() { show_404(); }

        function view_edit(){

            $this->data['title'] = "Edit ".ucfirst($_GET['post_type']);      
            $this->data['post_type'] = $_GET['post_type'];     
            $this->data['post_action'] = "EDIT";     
            $this->data['post'] = $this->posts_model->view_edit_posts();
            $this->data['all_posts'] = $this->posts_model->view_all_posts();
            $this->layout->view('posts/edit_resort',$this->data);

        }

        function view_new(){

            $this->data['title'] = "Add ".$_GET['post_type'];
            $this->data['post_type'] = $_GET['post_type']; 
            $this->data['post_action'] = "NEW";
            $this->data['all_posts'] = $this->posts_model->view_all_posts();
            $this->layout->view('posts/edit_resort',$this->data); 

        }

        function view_all(){

            if(!isset($_GET['post_type']))$_GET['post_type']='resort';

            $this->data['title'] = "View All ".$_GET['post_type'];
            $this->data['post_type'] = $_GET['post_type'];
            $this->data['all_posts'] = $this->posts_model->view_all_posts(); 
            $this->data['fields'] = $this->posts_model->get_fields(); 
            $this->layout->view('posts/view_all_resorts',$this->data); 

        } 

        function submit_post(){

            switch($_POST['post_action']){

                case 'NEW':     $this->posts_model->create_post();
                    break;

                case 'EDIT':    $this->posts_model->update_post();
                    break;

            }

        }

        function delete($post_ID){

            if($this->posts_model->delete_post($post_ID)){
                $this->view_all();
            }else{
                echo 'Error ocured';
                $this->view_all();
            }

        }

    }

    /* End of file Posts.php */
    /* Location: ./application/controllers/Posts.php */